package de.hauck.rtmidlet.model;

import java.util.Calendar;
import java.util.Date;

import de.hauck.rtmidlet.utils.MyLogger;
import de.hauck.rtmidlet.utils.Utils;

public class IBBean {
private String dateTimeStr;
private String buysell;
private int quantity;
private double price;
private double amount;
//private Date date;
//private Date time;

public String getDate() {
	//System.out.println(dateTimeStr);
	Date date = Utils.convertString2Date(dateTimeStr.substring(0,dateTimeStr.indexOf(",")));
	return Utils.convertDate2String(date);
}

public String getTime() {
	//System.out.println(dateTimeStr);
	Calendar cal = Calendar.getInstance();
	int index=dateTimeStr.indexOf(",")+2;
//	System.out.println(Integer.parseInt(dateTimeStr.substring(index,index+2)));
//	System.out.println(Integer.parseInt(dateTimeStr.substring(index+3,index+5)));
//	System.out.println(Integer.parseInt(dateTimeStr.substring(index+6,index+8)));
	cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(dateTimeStr.substring(index,index+2)));
	cal.set(Calendar.MINUTE, Integer.parseInt(dateTimeStr.substring(index+3,index+5)));
	cal.set(Calendar.SECOND, Integer.parseInt(dateTimeStr.substring(index+6,index+8)));
	cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)+Integer.parseInt(Configuration.timeOffset));
	return Utils.convertCal2Time(cal, true);
}
public String getDateTimeStr() {
	return dateTimeStr;
}
public void setDateTimeStr(String dateTimeStr) {
	this.dateTimeStr = dateTimeStr;
}
public String getBuySell() {
	return buysell;
}
public void setBuySell(String buysell) {
	this.buysell = buysell;
}

public int getQuantity() {
	return quantity;
}
public void setQuantity(int quantity) {
	this.quantity = quantity;
}
public double getPrice() {
	return price;
}
public void setPrice(double price) {
	this.price = price;
}
public double getAmount() {
	return amount;
}
public void setAmount(double amount) {
	this.amount = amount;
} 

public void setToken(int i, String token) {

	switch (i) {
	case 0: {
		setDateTimeStr(token);
		break;
	}
	case 1: {
		setBuySell(token);
		break;
	}
	
	case 2: {
		int in = 0;
		try {
			in = Integer.parseInt(token);
		} catch (NumberFormatException ex) {
			MyLogger.error(ex.getMessage());
		} finally {
			setQuantity(in);
		}
		break;
	}
	case 3: {
		double l = 0;
		try {
			l = Double.parseDouble(token);
		} catch (NumberFormatException ex) {
			MyLogger.error(ex.getMessage());
		} finally {
			setPrice(l);
		}
		break;
	}
	case 4: {
		double l = 0;
		try {
			l = Double.parseDouble(token);
		} catch (NumberFormatException ex) {
			MyLogger.error(ex.getMessage());
		} finally {
			setAmount(l);
		}
		break;
	}	
	}

}

public void printBean() {
	System.out.println("Bean: [\"" + dateTimeStr + "\", " + "\"" + buysell+ "\", "+quantity + "\", "
			+ "\"" + price + "\", " + "\"" + amount + "\"]");
}
}
